# -*- coding: utf-8 -*-

"""
Description:
"""
# from csm.basic import RequestHanlder

import time

from csm.index import CasmartApi
from utils.file_operation import load_json_to_dict

csm = CasmartApi()

data_list = load_json_to_dict("res_list.json")


def gen_product(pro_dict):
    """rj多规格生成csm商品"""
    # 处理图片链接
    _img_url = pro_dict["photos"][0]["photo"]
    # if _img_url.split("/")[-1] == "54415c0d-3391-45b3-b5e0-806115f68709.png":
    #     _img_url = "https://bio-mercury-1316961827.cos.ap-guangzhou.myqcloud.com/photos/huankai/huankai_product.jpg"

    _img_list = [{"path": _img_url, "type": 1} for _ in range(5)]

    _temp_list = []
    for item in pro_dict["skuInfos"]:
        # 商品基础信息
        _basic_dict = {
            "cateId": "385",
            "cateName": "实验试剂/常用生化试剂",
            "typeId": "300004006",
            "name": pro_dict["name"],
            "code": item["code"],
            "spec": item["sku"].split(":")[-1] + "/" + item["unit"],
            "brandId": "300179053",
            "unit": item["unit"],
            "maker": "广东环凯生物科技有限公司",
            "price": item["price"],
            "mktPrice": item["directoryPrice"],
            "deliveryCycle": "1",
            "stockAmount": "999",
            "groupNum": "1",
            "insaleStatus": "1",
            "giftStatus": "0",
            "deliveryId": "400000105",  # 运费模板
            "img": _img_url,
        }

        # 附加属性
        _extend_list = [{"extendId": "300095006", "value": "联系客服"}]

        # 商品详情
        _product_intro = {
            "content": pro_dict["desc"],
        }

        _temp_list.append(
            {
                "productBasic": _basic_dict,
                "extendList": _extend_list,
                "productIntro": _product_intro,
                "imgList": _img_list,
                "productCategoryList": [],
            }
        )
    return _temp_list


def gen_add_product(pro_dict):
    """生成添加商品的请求数据"""
    _temp_list = []
    _img_url = pro_dict["photos"][0]["photo"]
    for item in pro_dict["skuInfos"]:

        if "/" in item["sku"].split(":")[-1]:
            _spec = item["sku"].split(":")[-1]
        else:
            _spec = item["sku"].split(":")[-1] + "/" + item["unit"]

        _temp_list.append(
            {
                "rid": item["spuId"],
                "code": item["code"],
                "cateid": 501,
                "groupnum": 1,
                # 品牌id
                "brandid": 300639929,
                # 商品类型id
                "typeid": 300004006,
                "supplierid": 1,
                "name": pro_dict["name"],
                "mktprice": item["directoryPrice"],
                "price": item["price"],
                "unit": item["unit"],
                "imgs": [_img_url for _ in range(5)],
                "stockamount": 99,
                "isinsale": 1,
                "intro": pro_dict["desc"],
                "spec": _spec,
                "maker": "艾科瑞生物公司",
                "deliverycycle": 7,
                "cascode": "",
                "extends": [
                    {"key": 300095006, "value": "联系客服"},
                ],
                # "groups": [1],
            }
        )

    return _temp_list


# res = csm.get_cate_list()

# print(res)


for item in data_list:
    # print(item["code"])
    _temp_list = gen_add_product(item)
    for sku in _temp_list:
        res = csm.add_product(sku)
        print(res.json())
        time.sleep(1)
